package c.c.k.service.impl;

import c.c.k.config.StockConfig;
import c.c.k.emums.JYSEnum;
import c.c.k.pojo.FundBase;
import c.c.k.pojo.FundStock;
import c.c.k.pojo.Stock;
import c.c.k.pojo.StockBase;
import c.c.k.repository.FundBaseRepository;
import c.c.k.repository.FundStockRepository;
import c.c.k.repository.StockBaseRepository;
import c.c.k.repository.StockRepository;
import c.c.k.service.FundBaseService;
import c.c.k.service.FundStockService;
import c.c.k.service.StockBaseService;
import c.c.k.service.StockService;
import c.c.k.util.StockUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chenck on 2022/3/19.
 */
@Service
@Slf4j
public class FundStockServiceImpl implements FundStockService {
    @Autowired
    private FundBaseRepository fundBaseRepository;
    @Autowired
    private StockConfig stockConfig;
    @Autowired
    private FundStockRepository fundStockRepository;
    @Autowired
    private StockBaseRepository stockBaseRepository;

    @Override
    public List<FundStock> listByFundId(Long fundId) {
        return fundStockRepository.findByFundId(fundId, Sort.by("percent").descending());
    }

    @Override
    public List<FundStock> listByStockId(Long stockId, Double gtPercent) {
        return fundStockRepository.findByStockIdAndPercentGreaterThan(stockId, gtPercent);
    }

    @Override
    public List<FundStock> listByGtPercent(Double gtPercent) {
        return fundStockRepository.findByPercentGreaterThan(gtPercent);
    }

    @Override
    public void initFundStock() {
        int page = 1;
        while (true) {
            Page<FundBase> fundBases = fundBaseRepository.findAll(PageRequest.of(page++, 100));
            if (fundBases.getSize() == 0) {
                break;
            }
            fundBases.forEach(f -> {
                try {
                    StockUtil.parseJsonArray(String.format(stockConfig.getFundStockUrl(), f.getCode()), jsonArray -> {
                        for (int i = 0; i < jsonArray.size() && i < 10; i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String dm = jsonObject.getStr("dm");
                            String jzbl = jsonObject.getStr("jzbl");
                            jzbl = jzbl.substring(0, jzbl.length() - 1);

                            StockBase stockBase = stockBaseRepository.findByCode(dm);
                            if (stockBase != null) {
                                FundStock fundStock = fundStockRepository.findByStockIdAndFundId(stockBase.getId(), f.getId());
                                if (fundStock == null) {
                                    fundStockRepository.save(FundStock.builder()
                                            .fundId(f.getId())
                                            .percent(Double.parseDouble(jzbl))
                                            .stockId(stockBase.getId())
                                            .build());
                                }
                            }else {
                                log.error("===============stockBase is null==================");
                            }

                        }
                    });
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            });
        }
    }
}
